package com.aquan.map_jetpack.ui.components


import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.aquan.map_jetpack.compositionLocal.LocalUserViewModel
import com.aquan.map_jetpack.ui.navigation.Destinations
import com.aquan.map_jetpack.ui.screens.DetailsScreen
import com.aquan.map_jetpack.ui.screens.LoginScreen
import com.aquan.map_jetpack.ui.screens.MainBottomTab
import com.aquan.map_jetpack.ui.screens.MainFrame
import com.aquan.map_jetpack.viewmodel.UserViewModel
import com.google.accompanist.navigation.animation.AnimatedNavHost
import com.google.accompanist.navigation.animation.composable
import com.google.accompanist.navigation.animation.rememberAnimatedNavController

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun NavHostApp() {

    val navController = rememberAnimatedNavController()
//官方已经移除
//    ProvideWindowInsets() {


    CompositionLocalProvider(LocalUserViewModel provides UserViewModel(LocalContext.current)) {

        // 用户信息
        val userViewModel = LocalUserViewModel.current



        AnimatedNavHost(
            navController = navController,
            startDestination = if (userViewModel.userInfo == null) {
                Destinations.HomeFrame.route
            } else {
                Destinations.HomeFrame.route
            }
        ) {


            composable(
                Destinations.LoginFrame.route,
                enterTransition = {
                    slideIntoContainer(AnimatedContentScope.SlideDirection.Left)
                },
                exitTransition = {
                    slideOutOfContainer(AnimatedContentScope.SlideDirection.Right)
                }
            ) {
                LoginScreen(
                    onNavigateTo = {
                        navController.navigate(Destinations.HomeFrame.route)
                    }
                )
            }


            composable(
                Destinations.HomeFrame.route,
                enterTransition = {
                    slideIntoContainer(AnimatedContentScope.SlideDirection.Left)
                },
                exitTransition = {
                    slideOutOfContainer(AnimatedContentScope.SlideDirection.Right)
                }
            ) {
//                MainFrame(
//                    onNavigateTo = {
//                        navController.navigate(Destinations.DetailsFrame.route) {
////                            restoreState = true
//                            popUpTo(navController.graph.startDestinationId) {
//                                saveState = true
//                            }
//                            launchSingleTop = true
//                            restoreState = true
//                        }
//                    },
//                    onNavigateToLogin = {
//                        navController.navigate(Destinations.LoginFrame.route)
//                    }
//
//                )
                MainBottomTab()
            }


            composable(
                Destinations.DetailsFrame.route,
                enterTransition = {
                    slideIntoContainer(AnimatedContentScope.SlideDirection.Left)
                },
                exitTransition = {
                    slideOutOfContainer(AnimatedContentScope.SlideDirection.Right)
                }
            ) {
                DetailsScreen(backTo = {
                    navController.popBackStack()
                })
            }


            composable(
                Destinations.MainBottomTab.route,
                enterTransition = {
                    slideIntoContainer(AnimatedContentScope.SlideDirection.Left)
                },
                exitTransition = {
                    slideOutOfContainer(AnimatedContentScope.SlideDirection.Right)
                }
            ) {
                MainBottomTab()
            }

        }

//        Box(modifier = Modifier.fillMaxWidth()) {
//            Loading()
//        }
    }
}

//}

@Preview
@Composable
fun NavHostAppPreview() {
    NavHostApp()
}

